package com.jumeng.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jumeng.entity.CourseEntity;
import com.jumeng.entity.CourseUserReleationEntity;
import com.jumeng.entity.OrderEntity;
import com.jumeng.mapper.CourseMapper;
import com.jumeng.mapper.CourseUserRelationMapper;
import com.jumeng.mapper.OrderMapper;
import com.jumeng.server.CourseService;
import com.jumeng.server.CourseUserRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service
@Slf4j
public class CourseUserRelationServiceImpl extends ServiceImpl<CourseUserRelationMapper, CourseUserReleationEntity> implements CourseUserRelationService {

    @Autowired
    private CourseUserRelationMapper courseUserRelationMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public void saveCourseAndUserRelation(String orderNo) {
        LambdaQueryWrapper<OrderEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(OrderEntity::getOrderNo, orderNo);
        OrderEntity orderEntity = orderMapper.selectOne(wrapper);
        log.error("查询的订单,{}", orderEntity);
        if (orderEntity != null) {
            // 课程编码
            String courseCode = orderEntity.getCourseCode();
            // 账户编码
            String accountStationNo = orderEntity.getAccountStationNo();
            CourseUserReleationEntity courseUserReleationEntity = new CourseUserReleationEntity();
            courseUserReleationEntity.setCode(UUID.randomUUID().toString());
            courseUserReleationEntity.setCourseCode(courseCode);
            courseUserReleationEntity.setAccountStationNo(accountStationNo);
            courseUserReleationEntity.setCreatedBy(accountStationNo);
            courseUserReleationEntity.setUpdateBy(accountStationNo);
            courseUserReleationEntity.setCreatedTime(new Date());
            courseUserReleationEntity.setUpdatedTime(new Date());
            courseUserRelationMapper.insert(courseUserReleationEntity);
        }
    }
}
